package com.deyuanyun.pic.pipe.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.pipe.domain.MapValveRoomObj;
import com.deyuanyun.pic.pipe.domain.MapValveRoomObjExample;
import com.deyuanyun.pic.pipe.mapper.MapValveRoomObjMapper;
import com.deyuanyun.pic.pipe.mapper.dao.PipeValveRoomDao;
import com.deyuanyun.pic.pipe.service.PipeValveRoomService;
import com.deyuanyun.pic.settings.controller.dto.PipeValveRoomInfraVO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationQueryVO;
import com.deyuanyun.pic.settings.mapper.pipe.PipeLocationMapper;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;

@Service
public class PipeValveRoomServiceImpl implements PipeValveRoomService {
	
	@Resource
	private PipeValveRoomDao pipeValveRoomDao;
	
	private @Resource MapValveRoomObjMapper mapValveRoomObjMapper;
	private @Resource PipeLocationMapper pipeLocationMapper;
	
	public List<MapValveRoomObj> queryMapValveRoomObj(String pipeLineId){
	    
	    PipeLocationQueryVO pipeLocationQuery = new PipeLocationQueryVO();
	    pipeLocationQuery.setPipeLineId(pipeLineId);
	    pipeLocationQuery.setTargetType("valveRoomStation");
	    List<MapValveRoomObj> list = new ArrayList<MapValveRoomObj>();
	    List<PipeLocationDTO> pipeLocationList = pipeLocationMapper.queryPipeLocation(pipeLocationQuery);
	    for (int i = 0; i < pipeLocationList.size(); i++) {
	        String valveroomId = pipeLocationList.get(i).getId();
	        MapValveRoomObjExample example = new MapValveRoomObjExample();
	        example.createCriteria().andPipeValveroomIdEqualTo(valveroomId).andDeletedEqualTo((short)0).andIsNewestEqualTo((short)0);
	        List<MapValveRoomObj> list_t = mapValveRoomObjMapper.selectByExample(example);
	        list.addAll(list_t);
        }
	    return list;
	}
	
	@Override
	public Page<HashMap<String, Object>> findPage(HashMap param) {
		Page<HashMap<String, Object>> page= pipeValveRoomDao.findPage(param);
		for(HashMap<String, Object> map:page){
			map.put("SITE_TYPE", CodeUtil.getName((String)map.get("SITE_TYPE")));
		}
		return page;
	}

	@Override
	public Page<Object[]> findObjectList(HashMap param) {
		Page<LinkedHashMap<String,Object>> data= pipeValveRoomDao.findPageExport(param);
		Page<Object[]> result=new Page<Object[]>();
		for(int i=0;i<data.size();i++)
		{
			result.add(data.get(i).values().toArray());
		}
		result.setPageNum(data.getPageNum());
		result.setTotal(data.getTotal());
		return result;
	}
	/**
	* @Description: 新建站场阀室，包括：站场阀室名称、站场阀室代号
	* @author h.x.rong
	* @date 2015年12月23日
	* @param @param PipeValveRoomInfraVO
	* @return void 
	*
	 */
	public void insertValveRoom(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		//生成uuid，填到vo中
		pipeValveRoomInfraVO.setId(CommonUtil.getUUID());
		pipeValveRoomInfraVO.setTaskStatus("03");
		pipeValveRoomInfraVO.setStatus((short)1);
		pipeValveRoomDao.insertValveRoom(pipeValveRoomInfraVO);
		
	}

	/**
	* @Description: 条件查询，包括：站场阀室名称、站场阀室代号，任务状态、状态
	* @author h.x.rong
	* @date 2015年12月23日
	* @param @return
	* @return Page 
	*
	 */
	public Page<HashMap<String, Object>> findByCondition(
			PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		Page<HashMap<String, Object>> map = pipeValveRoomDao.findByCondition(pipeValveRoomInfraVO);
		for (HashMap<String, Object> m:map){
			m.put("TYPE_",CodeUtil.getName((String)m.get("TYPE_")));
		}
		return map;
	}

	/**
	* @Description: 批量删除(删除为假删除)
	* @author h.x.rong
	* @date 2015年12月23日
	* @param @param ids
	* @return void 
	 */
	public int dUpdateByIds(List ids) {
		// TODO Auto-generated method stub
		PipeValveRoomInfraVO pipeValveRoomInfraVO=new PipeValveRoomInfraVO();
		pipeValveRoomInfraVO.setIdList(ids);
		return pipeValveRoomDao.dUpdateByIds(pipeValveRoomInfraVO);
	}
	/**
	* @Title: deleteByIds 
	* @Description: 批量删除(删除为真删除)
	* @param @param id   
	* @user h.x.rong
	* 2015年12月30日
	 */
	public int deleteByIds(List ids) {
		PipeValveRoomInfraVO pipeValveRoomInfraVO=new PipeValveRoomInfraVO();
		pipeValveRoomInfraVO.setIdList(ids);
		return pipeValveRoomDao.deleteByIds(pipeValveRoomInfraVO);
	}
	
	/**
	* @Description: 删除未发布数据
	* @author h.x.rong
	* @date 2015年12月28日
	* @param @param id
	* @return void 
	*
	 */
//	public int removeVRById(String id) {
//		return	pipeValveRoomDao.deleteByPrimaryKey(id);
//
//	}

	/**
	* @Description: 假删除已发布数据
	* @author h.x.rong
	* @date 2015年12月28日
	* @param @param id
	* @return void 
	*
	 */
	public int shamRemoveVRById(String id) {
		PipeValveRoomInfraVO pipeValveRoomInfraVO=new PipeValveRoomInfraVO();
		pipeValveRoomInfraVO.setId(id);
		return pipeValveRoomDao.dUpdateByIds(pipeValveRoomInfraVO);
	}

	/**
	* @Description: 批量修改状态（发布）
	* @author h.x.rong
	* @date 2015年12月23日
	* @param 
	* @return void 
	 */
	public int updateStatus(List ids) {
		PipeValveRoomInfraVO pipeValveRoomInfraVO=new PipeValveRoomInfraVO();
		pipeValveRoomInfraVO.setIdList(ids);
		pipeValveRoomInfraVO.setTaskStatus("20");
		return pipeValveRoomDao.updateStatus(pipeValveRoomInfraVO);
	}

	/**
	* @Description: 站场阀室数据启用、停用
	* @author h.x.rong
	* @date 2015年12月30日
	* @param @param id
	* @param @param status
	* @return void 
	 */
	public int updateVRS(List id, short status) {
		PipeValveRoomInfraVO pipeValveRoomInfraVO=new PipeValveRoomInfraVO();
		pipeValveRoomInfraVO.setIdList(id);
		pipeValveRoomInfraVO.setStatus(status);
		return pipeValveRoomDao.updateVRS(pipeValveRoomInfraVO);
	}

	/**
	* @Description: 站场阀室修改、编辑
	* @author h.x.rong
	* @date 2015年12月31日
	* @param @param pipeValveRoomInfraVO
	* @return void 
	*
	 */
	public void updateVR(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		// TODO Auto-generated method stub
		pipeValveRoomDao.updateVR(pipeValveRoomInfraVO);
	}

	/**
	* @Description: 站场阀室验证重复（如：新增时需验证重复）
	* @author h.x.rong
	* @date 2015年12月31日
	* @param @return
	* @return List 
	 */
	public List validatRepByVR(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		// TODO Auto-generated method stub
		return pipeValveRoomDao.validatRepByVR(pipeValveRoomInfraVO);
	}

	/**
	* @Description: 站场阀室验证重复（修改时需验证重复）
	* @author h.x.rong
	* @date 2015年12月31日
	* @param @return
	* @return List 
	 */
	public List validatRepByVRU(PipeValveRoomInfraVO pipeValveRoomInfraVO) {
		// TODO Auto-generated method stub
		return pipeValveRoomDao.validatRepByVRU(pipeValveRoomInfraVO);
	}

	

	
}
